list1 = [1,2,3,4,5,6,7,8,9]

list2 = list()       #定义列表

list3 = [1,2,3,4,5,6,7,8,9,'aa''bb']
for i in list3:
    print(i,end=' ')
print(list3[:4])
print(list3[1:4])
print(list3[1:])
print(list3[::2])
print(list3[-1])                                   #序列切片

list3.append(6)
list3.extend([7,8,9])
list3.insert(0,0)
print(list3)


str = "hello world"
for i in str:
    print(i,end=' ')

print(" ")

# 列表排序
list4 = [76,28,13,45,55]
list4.sort()
print(list4)
list4.sort(reverse=True)
print(list4)

# 建立新列表，保留原列表
list_new = sorted(list4)
print(list_new)

#删除元素
del list4[0]
print(list4)

list4.append(13)
list4.append(13)
list4.remove(13)
print(list4)

#删除列表末尾的元素
list4.pop()

#清空列表
list4.clear()
print(list4)


#列表推导式
list5 = [11,23,3,4,15,36,7,18,9]
list5_new = [i for i in list5 if i%2 == 0]
print(list5_new)


# 传统for循环
list5_new2 = []
for i in list5:
    if i%2 == 0:
        list5_new2.append(i)
print(list5_new2)

tuple1 = (1,2,3,4,5,6,7,8,9)
print(tuple1)
tuple2 = (1,)
print(tuple2)
for i in tuple1:
    print(i,end=' ')

print(" ")

#集合
set1 = set([1,2,3,4,5,7])        #可以把列表重复的数据清除掉
print(set1)
set1.remove(1)
# set1.remove(11)    keyError
print(set1)
set1.discard(2)
print(set1)
set1.pop()
print(set1)
set1.clear()
print(set1)                                  # 集合推导式

# 字典  key:value 键值对
dict1 = {'name':'张三','age':18,'sex':'男'}
print(dict1)
print(dict1['name'])
print(dict1.get('gender','没有相关数据'))
print(dict1.get('name','没有相关数据'))
print(type(dict1.keys()))           #keys(),values(),item()

list7 = list(dict1.keys())

print(list7)
for i in dict1.keys():
    print(i,end=' ')
print(" ")     #换行
#字典元素的添加
dict1['id'] = 1001
print(dict1)

dict1['id'] = 1010
print(dict1)

#更新字典元素  update()
# 删除元素  pop()   popitem()
dict1.pop('sex')
print(dict1)

item = dict1.popitem()             #item为元组类型
print(type(item))
print(dict1)

#‘+’ 运算
str_one = "hello "
str_two = "world"
print(str_one + str_two)
list_one = [1, 2, 3]
list_two = [4, 5, 6]
print(list_one + list_two)
tuple_one = (1, 2, 3)
tuple_two = (3, 4, 5)
print(tuple_one + tuple_two)

#‘*’ 运算   倍增
list_one = [1, 2, 3]
print(list_one * 3)

print(1 in list_one)
print(1 not in list_one)